<template>
  <div>
    <div class="accounts-mokuai">
      <div class="accounts-index">
        <div class="accounts-name">
          <div class="accounts-name-font">账户设置</div>
        </div>
        <div class="accounts-content">
          <!-- 上传头像 -->
          <div class="accounts-img">
            <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
          <div class="accounts-img-font">
            上传头像
          </div>
          <!-- 用户设置 -->
          <div class="accounts-setting">
            <el-form
              :model="ruleForm"
              status-icon
              :rules="rules"
              ref="ruleForm"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="账户" prop="username">
                <el-input v-model.number="ruleForm.username"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" prop="email">
                <el-input v-model.number="ruleForm.email"></el-input>
              </el-form-item>
              <el-form-item label="旧密码" prop="oldPass">
                <el-input
                  type="password"
                  v-model="ruleForm.oldPass"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="新密码" prop="pass">
                <el-input
                  type="password"
                  v-model="ruleForm.pass"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="确认密码" prop="checkPass">
                <el-input
                  type="password"
                  v-model="ruleForm.checkPass"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item class="button-center">
                <el-button type="primary" @click="submitForm('ruleForm')"
                  >提交</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style>
/* 自定义 */

.accounts-mokuai {
  height: 700px;
  background-color: rgb(249, 249, 249);
  padding-top: 30px;
}
.accounts-index {
  width: 80%;
  margin: auto;
  background-color: rgb(255, 255, 255);
}
.accounts-name {
  width: 100%;
  height: 40px;
  background-color: rgb(240, 240, 240);
}
.accounts-name-font {
  height: 40px;
  line-height: 40px;
  margin-left: 20px;
  font-size: 14px;
}
.accounts-content {
  width: 100%;
  height: 550px;
}
.accounts-img {
  text-align: center;
  padding-top: 40px;
}
.accounts-img-font {
  text-align: center;
  color: #409eff;
}
.accounts-setting {
  margin-top: 20px;
  margin-left: 25%;
  width: 39%;
}

.button-center {
  margin-left: 20%;
}
/* 上传头像 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 20px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>

<script>
export default {
  data() {
    // 表单
    var checkUername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"))
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入电话号码"))
        } else {
          if (value < 11) {
            callback(new Error("请输入手机号"))
          } else {
            callback()
          }
        }
      }, 1000)
    }
    var checkEmail = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("邮箱不能为空"))
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入邮箱,暂时是数字"))
        } else {
          if (value < 11) {
            callback(new Error("请输入邮箱"))
          } else {
            callback()
          }
        }
      }, 1000)
    }

    var validateOldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入旧密码密码"))
      } else {
        // if (this.ruleForm.OldPass !== '') {
        //   this.$refs.ruleForm.validateField('OldPass');
        // }
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"))
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass")
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"))
      } else {
        callback()
      }
    }
    return {
      // 上传头像
      imageUrl: "",
      // 表单
      ruleForm: {
        username: "",
        email: "",
        oldPass: "",
        pass: "",
        checkPass: ""
      },
      rules: {
        username: [{ validator: checkUername, trigger: "blur" }],
        email: [{ validator: checkEmail, trigger: "blur" }],
        oldPass: [{ validator: validateOldPass, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }]
      }
    }
  },
  methods: {
    // 上传头像
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg"
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!")
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!")
      }
      return isJPG && isLt2M
    },

    // 表单
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!")
        } else {
          console.log("error submit!!")
          return false
        }
      })
    }
  }
}
</script>
